package com.example.himalaya.adapters;

/**
 * @Author Li Yue
 * @Date 2021/3/22
 **/

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.example.himalaya.R;
import com.example.himalaya.views.ColorFlipPagerTitleView;

import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

public class IndicatorAdapter extends CommonNavigatorAdapter {

    private final String[] mTiles;
    private OnIndicatorTapClickListener mOnTabClickListener;

    public IndicatorAdapter(Context context) {
        mTiles = context.getResources().getStringArray(R.array.indicator_name);
    }

    @Override
    public int getCount() {
        return mTiles == null ? 0 : mTiles.length;
    }

    @Override
    public IPagerTitleView getTitleView(Context context, final int index) {
        //创建view
        SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
        //设置要显示的内容
        simplePagerTitleView.setText(mTiles[index]);
        //设置一般情况下的颜色
        simplePagerTitleView.setNormalColor(Color.GRAY);
        //设置选中情况下的颜色
        simplePagerTitleView.setSelectedColor(Color.parseColor("#00c853"));
        //设置点击事件
        simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    //切换viewpage的内容

                if(mOnTabClickListener!=null){
                    mOnTabClickListener.onTabClick(index);
                }
            }
        });
        //设置创建好的view
        return simplePagerTitleView;
    }

    @Override
    public IPagerIndicator getIndicator(Context context) {
        LinePagerIndicator indicator =new LinePagerIndicator(context);
        indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
        indicator.setLineHeight(UIUtil.dip2px(context,6));
        indicator.setLineWidth(UIUtil.dip2px(context,10));
        indicator.setRoundRadius(UIUtil.dip2px(context,3));
        indicator.setStartInterpolator(new DecelerateInterpolator(2.0f));
        indicator.setColors(Color.parseColor("#00c853"));
        return indicator;
    }
    public  void  setOnIndicatorTapClickListener(OnIndicatorTapClickListener listener){
        this.mOnTabClickListener =listener;
    }

    public interface OnIndicatorTapClickListener{
        void onTabClick(int index);
    }
}
